package service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import util.Utilities;

import dao.AuctionDAO;
import dto.AuctionDTO;
import dto.BoardDTO;
import dto.ListAuctionDTO;
import dto.ListBoardDTO;
import dto.MemberDTO;
import dto.PagingDTO;
import dto.ProductDTO;

public class AuctionManagerService {
	private AuctionDAO auctionDAO;

	public AuctionManagerService(AuctionDAO auctionDAO) {
		this.auctionDAO = auctionDAO;
	}

	public void registerAuction(AuctionDTO auctionDTO, MemberDTO memberDTO) {
		auctionDTO.setMemberDTO(memberDTO);
		int auctionNo = auctionDAO.selectMaxAuctionNo();

		auctionDTO.setAuctionNo(auctionNo);

		auctionDTO.setAuctionContent(Utilities.changeContentForDB(auctionDTO
				.getAuctionContent()));
		System.out.println(auctionDTO.getAuctionContent());
		
		auctionDTO.setRegisterDate(Utilities.getNow());
		auctionDTO.setUseStartDate(Utilities.changeDateFormatForDB(auctionDTO
				.getUseStartDate()));
		auctionDTO.setUseEndDate(Utilities.changeDateFormatForDB(auctionDTO
				.getUseEndDate()));

		auctionDAO.insertAuction(auctionDTO);

		auctionDTO.setRegisterDate(Utilities.changeDayFormat(auctionDTO
				.getRegisterDate()));
	}

	public List getLocationList() {
		return auctionDAO.selectLocations();
	}

	public ArrayList<AuctionDTO> getAuctionList() {
		ArrayList<AuctionDTO> list = auctionDAO.selectAllAuctions();

		for (AuctionDTO a : list) {
			a.setRegisterDate(Utilities.changeDayTimeFormat(a.getRegisterDate()));
			a.setUseStartDate(Utilities.changeDayFormat(a.getUseStartDate()));
			a.setUseEndDate(Utilities.changeDayFormat(a.getUseEndDate()));
		}

		return list;
	}

	public ListAuctionDTO getMyAuctionList(String id, int page) {
		ArrayList<AuctionDTO> list = auctionDAO.selectAuctionsByMemberId(id, page);
		int totalContent = auctionDAO.selectTotalContentByMemberId(id);
		for (AuctionDTO a : list) {
			a.setRegisterDate(Utilities.changeDayTimeFormat(a.getRegisterDate()));
			a.setUseStartDate(Utilities.changeDayFormat(a.getUseStartDate()));
			a.setUseEndDate(Utilities.changeDayFormat(a.getUseEndDate()));
		}
		
		ListAuctionDTO listDTO = new ListAuctionDTO(list, new PagingDTO(totalContent, page)); 

		return listDTO;

		

	}

	public AuctionDTO getAuctionByAuctionNo(int auctionNo) {
		AuctionDTO auction = auctionDAO.selectAuctionByNo(auctionNo);

		auction.setRegisterDate(Utilities.changeDayTimeFormat(auction
				.getRegisterDate()));
		auction.setUseStartDate(Utilities.changeDayFormat(auction
				.getUseStartDate()));

		auction.setUseEndDate(Utilities.changeDayFormat(auction.getUseEndDate()));

		return auction;
	}

	public ListAuctionDTO getAuctionListByAuctionName(String auctionName, int page) {
		ArrayList<AuctionDTO> list = auctionDAO
				.selectAuctionsByAuctionName(auctionName, page);
		int totalContent = auctionDAO.selectTotalContentByAuctionName(auctionName);
		
		for (AuctionDTO a : list) {
			a.setRegisterDate(Utilities.changeDayTimeFormat(a.getRegisterDate()));
			a.setUseStartDate(Utilities.changeDayFormat(a.getUseStartDate()));
			a.setUseEndDate(Utilities.changeDayFormat(a.getUseEndDate()));
		}
		
		ListAuctionDTO listDTO = new ListAuctionDTO(list, new PagingDTO(totalContent, page));
		
		System.out.println(listDTO.getPagingDTO().getCurrentPage());
		return listDTO;

	}

	public ListAuctionDTO getAuctionListByCategory(String category, int page) {
		ArrayList<AuctionDTO> list = auctionDAO
				.selectAuctionsByCategory(category, page);
		int totalContent = auctionDAO.selectTotalContentByCategory(category);
		
		for (AuctionDTO a : list) {
			a.setRegisterDate(Utilities.changeDayTimeFormat(a.getRegisterDate()));
			a.setUseStartDate(Utilities.changeDayFormat(a.getUseStartDate()));
			a.setUseEndDate(Utilities.changeDayFormat(a.getUseEndDate()));
		}

		ListAuctionDTO listDTO = new ListAuctionDTO(list, new PagingDTO(totalContent, page));
		return listDTO;
	}

	public ListAuctionDTO getAuctionListByPage(int page) {
		ListAuctionDTO list = null;
		
		ArrayList<AuctionDTO> auctionList = auctionDAO.selectAuctionListByPage(page);
		int totalContent = auctionDAO.selectTotalContent(); 
		
		for (AuctionDTO a : auctionList) {
			a.setRegisterDate(Utilities.changeDayTimeFormat(a.getRegisterDate()));
			a.setUseStartDate(Utilities.changeDayFormat(a.getUseStartDate()));
			a.setUseEndDate(Utilities.changeDayFormat(a.getUseEndDate()));
		}
		
		
		list = new ListAuctionDTO(auctionList, new PagingDTO(totalContent, page));
		
		

		return list;
	}

	public ArrayList<String> getCategory() {
		return auctionDAO.selectCategoryList();
	}

	public void updateAuction(AuctionDTO auctionDTO, MemberDTO member) {
		auctionDTO.setMemberDTO(member);
		auctionDTO.setUpdateDate(Utilities.getNow());
		auctionDTO.setUseStartDate(Utilities.changeDateFormatForDB(auctionDTO.getUseStartDate()));
		auctionDTO.setUseEndDate(Utilities.changeDateFormatForDB(auctionDTO.getUseEndDate()));
		auctionDAO.updateAuction(auctionDTO);
	}

	public void removeAuction(int auctionNo) {
		auctionDAO.deleteAuctionByNo(auctionNo);
	}

}
